package org.oschina.api.discuss.forum.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.discuss.forum.model.DiscussForum;
import org.oschina.api.discuss.forum.service.DiscussForumIndexService;
import org.oschina.api.discuss.forum.service.DiscussForumService;
import org.oschina.api.discuss.forum.vo.DiscussForumIndexVo;
import org.oschina.api.discuss.forum.vo.DiscussForumVo;
import org.oschina.api.discuss.tag.service.DiscussTagService;
import org.oschina.api.discuss.tag.vo.DiscussTagVo;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/discuss/forum")
public class DiscussForumController extends BaseController {

	@Autowired
	private DiscussForumService discussForumService;
	@Autowired
	private DiscussTagService discussTagService;
	@Autowired
	private DiscussForumIndexService discussForumIndexService;
	
	/**
	 * 返回板块标签
	 * @param forum
	 * @return
	 */
	@Validate
	@RequestMapping(value="/tag/{forum}")
	public List<DiscussTagVo> findForumTag(@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer forum)
	{
		return discussTagService.findByVoDiscussForum(forum);
	}
	
	/**
	 * 返回
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<DiscussForumVo> findVo()
	{
		return discussForumService.findVo();
	}
	
	/**
	 * 返回首页数据
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public List<DiscussForumIndexVo> findIndexVo()
	{
		return discussForumIndexService.findVo();
	}
	
	/**
	 * 创建
	 * @param discussForum
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="check"),
			@ValidateField(name="index",validateValue=true,min=1),
			@ValidateField(name="index_type",validateValue=true,min=1)
		}) DiscussForum discussForum
	)
	{
		return discussForumService.create(discussForum);
	}
	
	/**
	 * 更新
	 * @param discussForum
	 * @return
	 */
	@Validate
	@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="check"),
			@ValidateField(name="index",validateValue=true,min=1),
			@ValidateField(name="index_type",validateValue=true,min=1)
		}) DiscussForum discussForum
	)
	{
		return discussForumService.update(discussForum);
	}
}